package com.dems.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dems.dao.dataobject.CfgArea;
import com.dems.dao.dataobject.ResRoom;
import com.dems.dao.dataobject.User;
import com.dems.dao.mapper.la.CfgAreaMapper;
import com.dems.dao.mapper.la.ResRoomMapper;
import com.dems.domain.ResRoomReqVO;
import com.dems.service.ResRoomService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ResRoomServiceImpl implements ResRoomService {
    @Autowired
    private ResRoomMapper resRoomMapper;

    @Autowired
    private CfgAreaMapper cfgAreaMapper;

    @Override
    public Page<ResRoom> find(ResRoomReqVO resRoomReqVO) {
        Page<ResRoom> page = new Page<>(resRoomReqVO.getPageNum(), resRoomReqVO.getPageSize());
        ResRoom resRoom = new ResRoom();
        BeanUtils.copyProperties(resRoomReqVO, resRoom);
        return resRoomMapper.findRoomList(page, resRoom);
    }

    @Override
    public ResRoom findOne(ResRoom resRoom) {
        return resRoomMapper.selectByPrimaryKey(resRoom.getId());
    }

    @Override
    public int createRoom(ResRoom resRoom, User user) {
        resRoom.setCreateBy(user.getName());
        resRoom.setCreateTime(new Date());
        resRoom.setCreateUserId(user.getUserId());
        resRoomMapper.insertSelective(resRoom);
        resRoom = resRoomMapper.selectByRoomName(resRoom.getRoomName());
        CfgArea cfgArea = new CfgArea();
        cfgArea.setNodeId(resRoom.getId());
        cfgArea.setNodeType("ROOM");
        cfgArea.setNodeName(resRoom.getRoomName());
        cfgArea.setNodeLevel(4);
        cfgArea.setParentId(resRoom.getRegionId());
        return cfgAreaMapper.insertSelective(cfgArea);
    }

    @Override
    public int updateRoom(ResRoom resRoom, User user) {
        resRoom.setUpdateBy(user.getName());
        resRoom.setUpdateTime(new Date());
        resRoom.setUpdateUserId(user.getUserId());
        return resRoomMapper.updateByPrimaryKeySelective(resRoom);
    }
}
